var RootUrl = "http://localhost:8080/";
var RootUrl_Https = "https://localhost:8443/";

function SendToPrinter (FuncName, JsonObject) {
	return SendToPrinter_Base(RootUrl, FuncName, JsonObject);
}

function SendToPrinter_Https (FuncName, JsonObject) {
	return SendToPrinter_Base(RootUrl_Https, FuncName, JsonObject);
}

function SendToPrinter_Base (BaseUrl, FuncName, JsonObject) {
	// Response Data
	var RetArray = [];

	// Combine WebAPI URL
	var SendUrl = BaseUrl + FuncName + "/";

	// Get Json Text
	var SendData = JSON.stringify(JsonObject);

	// Debug
	//alert("Url = " + SendUrl + "\r\nData = " + SendData);

	// Encrypt Data
	if (SendData != "") {
		SendData = $.base64.encode(SendData);
	}

	// Send Ajax Request
	$.ajax(
		{
			url: SendUrl,
			type: "POST",
			cache: false,
			dataType: 'json',
			data: { EncodeData: SendData },
			async: false,

			success: function (data) {
				// Get Json Text
				var JsonData = JSON.stringify(data);
				//alert(JsonData);

				// JsonData = {"AAA","BBB","CCC"}
				if (JsonData.length > 4) {
					// JsonData = AAA","BBB","CCC
					JsonData = JsonData.substr(2, JsonData.length - 4);
				}

				var ItemList = JsonData.split('\",\"');
				for (i = 0; i < ItemList.length; i++) {
					//alert(ItemList[i]);
					if (ItemList[i].length > 0 && ItemList[i] != "[]") {
						var str = $.base64.decode(ItemList[i]);
						RetArray.push(str);
					}
				}
			},

			error: function (xhr, ajaxOptions, thrownError) {
				//alert(xhr.status);
				//alert(thrownError);
			}
		});

	return RetArray;
}

function QueryUsbList () {
	return SendToPrinter("UsbList", "");
}

function QueryUsbList_Https () {
	return SendToPrinter_Https("UsbList", "");
}